package com.zhuiyi.yiqingbackend.controller;

import com.zhuiyi.yiqingbackend.common.BaseResponse;
import com.zhuiyi.yiqingbackend.common.ErrorCode;
import com.zhuiyi.yiqingbackend.common.ResultUtils;
import com.zhuiyi.yiqingbackend.domain.Person;
import com.zhuiyi.yiqingbackend.exception.BusinessException;
import com.zhuiyi.yiqingbackend.service.PersonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/person")
@Api(value = "人员接口", tags = "登记人员接口")
@CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
public class PersonController {
    @Resource
    private PersonService personService;

    /**
     * 获取所有人员
     *
     */
    @GetMapping("/getAllPerson")
    @ApiOperation(value = "获取所有人员", notes = "获取所有人员")
    public BaseResponse<List<Person>> getAllPerson() {
        List<Person> allPerson = personService.getAllPerson();
        return ResultUtils.success(allPerson);
    }

    /**
     * 添加人员
     *
     */
    @PostMapping("/insertPerson")
    @ApiOperation(value = "添加人员", notes = "添加人员")
    public BaseResponse<Boolean> insertPerson(@RequestBody Person person) {

        boolean ok = personService.insertPerson(person);
        if (!ok) {
            throw new BusinessException(ErrorCode.FAIL, "添加失败");
        }
        return ResultUtils.success(ok);
    }

    /**
     * 删除人员
     *
     */
    @GetMapping("/deletePerson/{id}")
    @ApiOperation(value = "删除人员", notes = "删除人员")
    public BaseResponse<Boolean> deletePerson(@PathVariable int id) {
        boolean ok = personService.deletePerson(id);
        if (!ok) {
            throw new BusinessException(ErrorCode.FAIL, "删除失败");
        }
        return ResultUtils.success(ok);
    }

    /**
     * 更新人员
     *
     */
    @PostMapping("/updatePerson")
    @ApiOperation(value = "更新人员", notes = "更新人员")
    public BaseResponse<Boolean> updatePerson(@RequestBody Person person) {
        boolean ok = personService.updatePerson(person);
        if (!ok) {
            throw new BusinessException(ErrorCode.FAIL, "更新失败");
        }
        return ResultUtils.success(ok);
    }
    /**
     * 根据id获取人员
     */
    @GetMapping("/getPersonById/{id}")
    @ApiOperation(value = "根据id获取人员", notes = "根据id获取人员")
    public BaseResponse<List<Person>> getPersonById(@PathVariable int id) {
        List<Person> personList = personService.getPersonById(id);
        return ResultUtils.success(personList);
    }
}
